package nc.bs.bd.inter;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 
 * 
 * @author LP
 *
 */

public class OASendHttpUtilPost {

	public static String sendOA(String ip,String json) throws Exception{
		URL url = new URL(ip);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
		connection.setRequestProperty("timestamp", System.currentTimeMillis()+"");
		connection.connect();

		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
		writer.write(json);
		writer.close();

		BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
		byte[] b = new byte[in.available()];
		String s = "";
		in.read(b);
		s = new String(b,"UTF-8");
		return s;
	}

	public static String sendOA(String ip,String token,String json) throws Exception{

		String urlNameString = ip + "?" + token;
		URL realUrl = new URL(urlNameString);

		HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
		connection.setRequestMethod("POST");
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setUseCaches(false);
		connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
		connection.setRequestProperty("timestamp", System.currentTimeMillis()+"");
		connection.connect();

		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
		writer.write(json);
		writer.close();

		BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
		byte[] b = new byte[in.available()];
		String s = "";
		in.read(b);
		s = new String(b,"UTF-8");
		return s;
	}
}

